//
//  MYSelectBulletView.m
//  JQLive
//
//  Created by cheng on 2020/6/17.
//  Copyright © 2020 JQLive. All rights reserved.
//

#import "MYSelectBulletView.h"
#import "MYSelectBulletCollectionViewCell.h"


@interface MYSelectBulletView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UIView *mainView;
    UILabel *titleLabel;
    NSInteger bottomDistance;
}
@property (nonatomic, copy) MYSelectBulletViewCompletion completion;
@property (nonatomic,strong) NSArray *itemArray;
@property (nonatomic,strong) UICollectionView* collectionView;
@end

@implementation MYSelectBulletView

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        flowLayout.minimumLineSpacing = 5;
        flowLayout.minimumInteritemSpacing = 5;
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
        flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH-40)/4, 80);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        [_collectionView registerClass:[MYSelectBulletCollectionViewCell class] forCellWithReuseIdentifier:@"MYSelectBulletCollectionViewCell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.scrollsToTop = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.layer.cornerRadius = 8;
        _collectionView.clipsToBounds = YES;
    }
    return _collectionView;
}

- (void)showWithNav:(UINavigationController *)nav
          itemArray:(NSArray *)array
         completion:(MYSelectBulletViewCompletion)completion
{
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self];
    self.completion = completion;
    
    if (array.count > 0)
    {
        self.itemArray = array;
//        self.itemArray = [self getDataWithTypeArray:array];
        [self.collectionView reloadData];
    }
    [self freashFrame];
    [UIView animateWithDuration:0.3 animations:^{
        self->mainView.transform = CGAffineTransformTranslate(self->mainView.transform, 0, -self->mainView.bounds.size.height);
    } completion:^(BOOL finished) {
    }];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    if(self = [super initWithFrame:frame])
    {
        [self layout];
    }
    
    return self;
}

- (void)layout
{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    
    bottomDistance = INTERFACE_IS_IPHONEX ? 34 : 0;
    
    mainView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-bottomDistance-330, SCREEN_WIDTH, 330+bottomDistance)];
    mainView.backgroundColor = [UIColor whiteColor];
    mainView.clipsToBounds = YES;
    [self addSubview:mainView];
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(330+bottomDistance);
    }];
    
    
    titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = MY_Text_Color_Theme1;
    titleLabel.text = @"选择";
    [mainView addSubview:titleLabel];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [closeBtn setTitle:@"取消" forState:0];
//    closeBtn.titleLabel.font = [UIFont systemFontOfSize:18];
//    []
    [closeBtn setImage:[UIImage imageNamed:@"liveroom_closebox"] forState:UIControlStateNormal];
//    [closeBtn setTitleColor:MY_ColourMatchingSpecifications_Theme1 forState:0];
//    closeBtn.backgroundColor = MY_ColourMatchingSpecifications_VCBack;
    [closeBtn addTarget:self action:@selector(cancelButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:closeBtn];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self->mainView);
        make.top.equalTo(@(MY_Padding));
    }];
    
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@30);
        make.right.equalTo(self->mainView);
        make.bottom.equalTo(self->mainView.mas_top).mas_equalTo(7);
    }];
    
    [mainView addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.equalTo(self->mainView);
        make.bottom.equalTo(closeBtn.mas_top).offset(-10);
        make.top.equalTo(self->titleLabel.mas_bottom).offset(34);
    }];
    
    //设置上面两个圆角
    UIBezierPath* maskPath = [UIBezierPath bezierPathWithRoundedRect:mainView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
    CAShapeLayer* maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = mainView.bounds;
    maskLayer.path = maskPath.CGPath;
    mainView.layer.mask = maskLayer;
    
    mainView.transform = CGAffineTransformTranslate(mainView.transform, 0, mainView.bounds.size.height);
}

- (void)freashFrame
{
    NSInteger lineMax = 4;
    NSInteger count = self.itemArray.count;
    NSInteger row = count/lineMax + ((count%lineMax)>0?1:0);
    CGFloat itemHeigh = 85;
    CGFloat mainHeight =  150 + row*itemHeigh;
    [mainView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(mainHeight +bottomDistance);
    }];
    
    DLog(@"%@",mainView);
}

#pragma mark - UICollectionView delgate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MYSelectBulletCollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MYSelectBulletCollectionViewCell" forIndexPath:indexPath];
    
    [cell updateContent:self.itemArray[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.completion) {
        self.completion(self, indexPath);
    }
    [self dismiss];
}

#pragma mark- 自定义方法

- (void)cancelButtonTouchUpInside:(UIButton *)sender
{
    [self dismiss];
}

- (void)dismiss
{
    //点击关闭
    [UIView animateWithDuration:0.20 animations:^{
        self->mainView.transform = CGAffineTransformTranslate(self->mainView.transform, 0, self->mainView.bounds.size.height);
        
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.20 animations:^{
            self.alpha = 0.0;
        } completion:^(BOOL finished) {
            self.hidden = YES;
            [self removeFromSuperview];
        }];
    }];
}

@end
